package com.ssy.lingxi.report.serviceimpl.tally;

import com.ssy.lingxi.report.api.enums.*;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import com.ssy.lingxi.report.dao.settleaccount.MemberSettlementDAO;
import com.ssy.lingxi.report.dao.settleaccount.PlatformScoreSettlementDAO;
import com.ssy.lingxi.report.dao.settleaccount.PlatformSettlementDAO;
import com.ssy.lingxi.report.entity.report.MemberTallyReportDO;
import com.ssy.lingxi.report.model.bo.MemberTallyReportBO;
import com.ssy.lingxi.report.model.bo.StatisticsSettlementBO;
import com.ssy.lingxi.report.model.bo.tally.SettleAccountPayableTallyBO;
import com.ssy.lingxi.report.model.bo.tally.SettleAccountPlatformScoreTallyBO;
import com.ssy.lingxi.report.model.bo.tally.SettleAccountPlatformTallyBO;
import com.ssy.lingxi.report.model.bo.tally.SettleAccountReceivableTallyBO;
import com.ssy.lingxi.report.model.vo.response.SettleAccountTallyReportVO;
import com.ssy.lingxi.report.model.vo.response.TallyReportItemVO;
import com.ssy.lingxi.report.service.tally.ISettleAccountTallyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 结算计数统计接口实现
 * @author 伍将
 * @date 2020/11/23
 * @version 2.0.0
 */
@Service
public class SettleAccountTallyServiceImpl implements ISettleAccountTallyService {

    private static final Logger logger = LoggerFactory.getLogger(SettleAccountTallyServiceImpl.class);

    @Autowired
    private PlatformSettlementDAO platformSettlementDAO;
    @Autowired
    private PlatformScoreSettlementDAO platformScoreSettlementDAO;
    @Autowired
    private MemberSettlementDAO memberSettlementDAO;


    /**
     * 设置结算
     * @param tallyReportO
     * @param noticeVO
     * @return
     */
    @Override
    public MemberTallyReportBO setSettleAccountTally(MemberTallyReportBO tallyReportO, OperateNoticeVO noticeVO) {
        OperateDataSourceEnum sourceEnum = OperateDataSourceEnum.getEnum(noticeVO.getDataSource());
        switch (sourceEnum) {
            case SETTLE_ACCOUNT_PAYABLE: // 应付款结算
                tallyReportO.setSettleAccountPayable(setSettleAccountPayableTally(tallyReportO.getSettleAccountPayable(), noticeVO));
                break;
            case SETTLE_ACCOUNT_RECEIVABLE: // 应收款结算
                tallyReportO.setSettleAccountReceivable(setSettleAccountReceivableTally(tallyReportO.getSettleAccountReceivable(), noticeVO));
                break;
            case SETTLE_ACCOUNT_PLATFORM: // 平台结算
                tallyReportO.setSettleAccountPlatform(setSettleAccountPlatformTally(tallyReportO.getSettleAccountPlatform(), noticeVO));
                break;
            case SETTLE_ACCOUNT_PLATFORM_SCORE: // 平台积分结算
                tallyReportO.setSettleAccountPlatformScore(setSettleAccountPlatformScoreTally(tallyReportO.getSettleAccountPlatformScore(), noticeVO));
                break;
        }
        return tallyReportO;
    }

    /**
     * 转换结算
     * @param reportBO
     * @return
     */
    @Override
    public SettleAccountTallyReportVO convertSettleAccount(MemberTallyReportBO reportBO) {
        SettleAccountTallyReportVO result = new SettleAccountTallyReportVO();

        // 转换应收款结算
        result.setReceivableList(convertSettleAccountReceivableTally(reportBO.getSettleAccountReceivable()));
        // 转换应付款结算
        result.setPayableList(convertSettleAccountPayableTally(reportBO.getSettleAccountPayable()));
        // 转换平台结算
        result.setPlatformList(convertSettleAccountPlatformTally(reportBO.getSettleAccountPlatform()));
        // 转换平台积分结算
        result.setPlatformScoreList(convertSettleAccountPlatformScoreTally(reportBO.getSettleAccountPlatformScore()));

        return result;
    }


    /**
     * 转换平台积分结算
     * @param tallyBO
     * @return
     */
    private List<TallyReportItemVO> convertSettleAccountPlatformScoreTally(SettleAccountPlatformScoreTallyBO tallyBO) {
        List<TallyReportItemVO> resultList = new ArrayList<>();
        if (tallyBO == null) {
            tallyBO = new SettleAccountPlatformScoreTallyBO();
        }
        // 待对账数
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeAccountsCount());
        tallyReportItem.setName(SettleAccountPlatformScoreOperateTypeEnum.TO_BE_ACCOUNTS.getName());
        tallyReportItem.setLink(SettleAccountPlatformScoreOperateTypeEnum.TO_BE_ACCOUNTS.getLink());
        resultList.add(tallyReportItem);
        // 待支付数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBePayCount());
        tallyReportItem.setName(SettleAccountPlatformScoreOperateTypeEnum.TO_BE_PAY.getName());
        tallyReportItem.setLink(SettleAccountPlatformScoreOperateTypeEnum.TO_BE_PAY.getLink());
        resultList.add(tallyReportItem);
        // 待收款数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeReceiptCount());
        tallyReportItem.setName(SettleAccountPlatformScoreOperateTypeEnum.TO_BE_RECEIPT.getName());
        tallyReportItem.setLink(SettleAccountPlatformScoreOperateTypeEnum.TO_BE_RECEIPT.getLink());
        resultList.add(tallyReportItem);
        // 完成数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getCompleteCount());
        tallyReportItem.setName(SettleAccountPlatformScoreOperateTypeEnum.COMPLETE.getName());
        tallyReportItem.setLink(SettleAccountPlatformScoreOperateTypeEnum.COMPLETE.getLink());
        resultList.add(tallyReportItem);

        resultList.forEach(o -> o.setCount(o.getCount() > 0 ? o.getCount() : 0));

        return resultList;
    }

    /**
     * 转换平台结算
     * @param tallyBO
     * @return
     */
    private List<TallyReportItemVO> convertSettleAccountPlatformTally(SettleAccountPlatformTallyBO tallyBO) {
        List<TallyReportItemVO> resultList = new ArrayList<>();
        if (tallyBO == null) {
            tallyBO = new SettleAccountPlatformTallyBO();
        }
        // 待对账数
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeAccountsCount());
        tallyReportItem.setName(SettleAccountPlatformOperateTypeEnum.TO_BE_ACCOUNTS.getName());
        tallyReportItem.setLink(SettleAccountPlatformOperateTypeEnum.TO_BE_ACCOUNTS.getLink());
        resultList.add(tallyReportItem);
        // 待支付数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBePayCount());
        tallyReportItem.setName(SettleAccountPlatformOperateTypeEnum.TO_BE_PAY.getName());
        tallyReportItem.setLink(SettleAccountPlatformOperateTypeEnum.TO_BE_PAY.getLink());
        resultList.add(tallyReportItem);
        // 待收款数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeReceiptCount());
        tallyReportItem.setName(SettleAccountPlatformOperateTypeEnum.TO_BE_RECEIPT.getName());
        tallyReportItem.setLink(SettleAccountPlatformOperateTypeEnum.TO_BE_RECEIPT.getLink());
        resultList.add(tallyReportItem);
        // 完成数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getCompleteCount());
        tallyReportItem.setName(SettleAccountPlatformOperateTypeEnum.COMPLETE.getName());
        tallyReportItem.setLink(SettleAccountPlatformOperateTypeEnum.COMPLETE.getLink());
        resultList.add(tallyReportItem);

        resultList.forEach(o -> o.setCount(o.getCount() > 0 ? o.getCount() : 0));

        return resultList;
    }

    /**
     * 转换应付款结算
     * @param tallyBO
     * @return
     */
    private List<TallyReportItemVO> convertSettleAccountPayableTally(SettleAccountPayableTallyBO tallyBO) {
        List<TallyReportItemVO> resultList = new ArrayList<>();
        if (tallyBO == null) {
            tallyBO = new SettleAccountPayableTallyBO();
        }
        // 待对账数
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeAccountsCount());
        tallyReportItem.setName(SettleAccountPayableOperateTypeEnum.TO_BE_ACCOUNTS.getName());
        tallyReportItem.setLink(SettleAccountPayableOperateTypeEnum.TO_BE_ACCOUNTS.getLink());
        resultList.add(tallyReportItem);
        // 待支付数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBePayCount());
        tallyReportItem.setName(SettleAccountPayableOperateTypeEnum.TO_BE_PAY.getName());
        tallyReportItem.setLink(SettleAccountPayableOperateTypeEnum.TO_BE_PAY.getLink());
        resultList.add(tallyReportItem);
        // 待收款数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeReceiptCount());
        tallyReportItem.setName(SettleAccountPayableOperateTypeEnum.TO_BE_RECEIPT.getName());
        tallyReportItem.setLink(SettleAccountPayableOperateTypeEnum.TO_BE_RECEIPT.getLink());
        resultList.add(tallyReportItem);
        // 完成数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getCompleteCount());
        tallyReportItem.setName(SettleAccountPayableOperateTypeEnum.COMPLETE.getName());
        tallyReportItem.setLink(SettleAccountPayableOperateTypeEnum.COMPLETE.getLink());
        resultList.add(tallyReportItem);

        resultList.forEach(o -> o.setCount(o.getCount() > 0 ? o.getCount() : 0));

        return resultList;
    }

    /**
     * 转换应收款结算
     * @param tallyBO
     * @return
     */
    private List<TallyReportItemVO> convertSettleAccountReceivableTally(SettleAccountReceivableTallyBO tallyBO) {
        List<TallyReportItemVO> resultList = new ArrayList<>();
        if (tallyBO == null) {
            tallyBO = new SettleAccountReceivableTallyBO();
        }
        // 待对账数
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeAccountsCount());
        tallyReportItem.setName(SettleAccountReceivableOperateTypeEnum.TO_BE_ACCOUNTS.getName());
        tallyReportItem.setLink(SettleAccountReceivableOperateTypeEnum.TO_BE_ACCOUNTS.getLink());
        resultList.add(tallyReportItem);
        // 待支付数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBePayCount());
        tallyReportItem.setName(SettleAccountReceivableOperateTypeEnum.TO_BE_PAY.getName());
        tallyReportItem.setLink(SettleAccountReceivableOperateTypeEnum.TO_BE_PAY.getLink());
        resultList.add(tallyReportItem);
        // 待收款数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeReceiptCount());
        tallyReportItem.setName(SettleAccountReceivableOperateTypeEnum.TO_BE_RECEIPT.getName());
        tallyReportItem.setLink(SettleAccountReceivableOperateTypeEnum.TO_BE_RECEIPT.getLink());
        resultList.add(tallyReportItem);
        // 完成数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getCompleteCount());
        tallyReportItem.setName(SettleAccountReceivableOperateTypeEnum.COMPLETE.getName());
        tallyReportItem.setLink(SettleAccountReceivableOperateTypeEnum.COMPLETE.getLink());
        resultList.add(tallyReportItem);
        // 待开票数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeInvoice());
        tallyReportItem.setName(SettleAccountReceivableOperateTypeEnum.TO_BE_INVOICE.getName());
        tallyReportItem.setLink(SettleAccountReceivableOperateTypeEnum.TO_BE_INVOICE.getLink());
        resultList.add(tallyReportItem);

        resultList.forEach(o -> o.setCount(o.getCount() > 0 ? o.getCount() : 0));

        return resultList;
    }


    /**
     * 设置平台积分结算
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private SettleAccountPlatformScoreTallyBO setSettleAccountPlatformScoreTally(SettleAccountPlatformScoreTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new SettleAccountPlatformScoreTallyBO();
        }
        // 更新数据
        SettleAccountPlatformScoreOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = SettleAccountPlatformScoreOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() - noticeVO.getReduceCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = SettleAccountPlatformScoreOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() + noticeVO.getIncreaseCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }

    /**
     * 设置平台结算
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private SettleAccountPlatformTallyBO setSettleAccountPlatformTally(SettleAccountPlatformTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new SettleAccountPlatformTallyBO();
        }
        // 更新数据
        SettleAccountPlatformOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = SettleAccountPlatformOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() - noticeVO.getReduceCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = SettleAccountPlatformOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() + noticeVO.getIncreaseCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }

    /**
     * 设置应收款结算
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private SettleAccountReceivableTallyBO setSettleAccountReceivableTally(SettleAccountReceivableTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new SettleAccountReceivableTallyBO();
        }
        // 更新数据
        SettleAccountReceivableOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = SettleAccountReceivableOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() - noticeVO.getReduceCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_INVOICE:
                        tallyBO.setToBeInvoice(tallyBO.getToBeInvoice() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = SettleAccountReceivableOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() + noticeVO.getIncreaseCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_INVOICE:
                        tallyBO.setToBeInvoice(tallyBO.getToBeInvoice() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }

    /**
     * 设置应付款结算
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private SettleAccountPayableTallyBO setSettleAccountPayableTally(SettleAccountPayableTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new SettleAccountPayableTallyBO();
        }
        // 更新数据
        SettleAccountPayableOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = SettleAccountPayableOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() - noticeVO.getReduceCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = SettleAccountPayableOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_ACCOUNTS:
                        tallyBO.setToBeAccountsCount(tallyBO.getToBeAccountsCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_PAY:
                        tallyBO.setToBePayCount(tallyBO.getToBePayCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_RECEIPT:
                        tallyBO.setToBeReceiptCount(tallyBO.getToBeReceiptCount() + noticeVO.getIncreaseCount());
                        break;
                    case COMPLETE:
                        tallyBO.setCompleteCount(tallyBO.getCompleteCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }


    /**
     * 统计计数
     * @param reportList
     * @return
     */
    @Override
    public List<MemberTallyReportDO> statisticsTally(List<MemberTallyReportDO> reportList) {

        // 平台代收账款结算
        reportList = statisticsPlatformTally(reportList);

        // 平台积分结算
        reportList = statisticsPlatformScoreTally(reportList);

        // 应付账款结算
        reportList = statisticsPayableTally(reportList);

        // 应收账款结算
        reportList = statisticsReceivableTally(reportList);

        return reportList;
    }

    /**
     * 统计应收款结算
     * @param reportList
     * @return
     */
    private List<MemberTallyReportDO> statisticsReceivableTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<StatisticsSettlementBO> statisticsList = memberSettlementDAO.getReceivableStatisticsCount();
        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = replenishData(reportList, statisticsList, "应收款结算");

        List<StatisticsSettlementBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            SettleAccountReceivableTallyBO tallyBO = new SettleAccountReceivableTallyBO();

            StatisticsSettlementBO statisticsBO = finalStatisticsList.stream().filter(
                    s -> s.getMemberId().equals(p.getMemberId()) && s.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (statisticsBO != null) {
                // 待对账
                tallyBO.setToBeAccountsCount(statisticsBO.getToBeAccountsCount());
                // 待支付
                tallyBO.setToBePayCount(statisticsBO.getToBePayCount());
                // 代收款
                tallyBO.setToBeReceiptCount(statisticsBO.getToBeReceiptCount());
                // 完成数
                tallyBO.setCompleteCount(statisticsBO.getCompleteCount());
                // 待开票数
                tallyBO.setToBeInvoice(statisticsBO.getToBeInvoice());
            }

            p.getTallyReport().setSettleAccountReceivable(tallyBO);
        });

        return finalReportList;
    }

    /**
     * 统计应付款结算
     * @param reportList
     * @return
     */
    private List<MemberTallyReportDO> statisticsPayableTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<StatisticsSettlementBO> statisticsList = memberSettlementDAO.getPayableStatisticsCount();
        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = replenishData(reportList, statisticsList, "应付款结算");

        List<StatisticsSettlementBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            SettleAccountPayableTallyBO tallyBO = new SettleAccountPayableTallyBO();

            StatisticsSettlementBO statisticsBO = finalStatisticsList.stream().filter(
                    s -> s.getMemberId().equals(p.getMemberId()) && s.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (statisticsBO != null) {
                // 待对账
                tallyBO.setToBeAccountsCount(statisticsBO.getToBeAccountsCount());
                // 待支付
                tallyBO.setToBePayCount(statisticsBO.getToBePayCount());
                // 代收款
                tallyBO.setToBeReceiptCount(statisticsBO.getToBeReceiptCount());
                // 完成数
                tallyBO.setCompleteCount(statisticsBO.getCompleteCount());
            }

            p.getTallyReport().setSettleAccountPayable(tallyBO);
        });

        return finalReportList;
    }

    /**
     * 统计平台积分结算
     * @param reportList
     * @return
     */
    private List<MemberTallyReportDO> statisticsPlatformScoreTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<StatisticsSettlementBO> statisticsList = platformScoreSettlementDAO.getStatisticsCount();
        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = replenishData(reportList, statisticsList, "平台代积分结算");

        List<StatisticsSettlementBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            SettleAccountPlatformScoreTallyBO tallyBO = new SettleAccountPlatformScoreTallyBO();

            StatisticsSettlementBO statisticsBO = finalStatisticsList.stream().filter(
                    s -> s.getMemberId().equals(p.getMemberId()) && s.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (statisticsBO != null) {
                // 待对账
                tallyBO.setToBeAccountsCount(statisticsBO.getToBeAccountsCount());
                // 待支付
                tallyBO.setToBePayCount(statisticsBO.getToBePayCount());
                // 代收款
                tallyBO.setToBeReceiptCount(statisticsBO.getToBeReceiptCount());
                // 完成数
                tallyBO.setCompleteCount(statisticsBO.getCompleteCount());
            }

            p.getTallyReport().setSettleAccountPlatformScore(tallyBO);
        });

        return finalReportList;
    }

    /**
     * 统计平台代收账款结算
     * @param reportList
     * @return
     */
    private List<MemberTallyReportDO> statisticsPlatformTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<StatisticsSettlementBO> statisticsList = platformSettlementDAO.getStatisticsCount();
        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = replenishData(reportList, statisticsList, "平台代收账款结算");

        List<StatisticsSettlementBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            SettleAccountPlatformTallyBO tallyBO = new SettleAccountPlatformTallyBO();

            StatisticsSettlementBO statisticsBO = finalStatisticsList.stream().filter(
                    s -> s.getMemberId().equals(p.getMemberId()) && s.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (statisticsBO != null) {
                // 待对账
                tallyBO.setToBeAccountsCount(statisticsBO.getToBeAccountsCount());
                // 待支付
                tallyBO.setToBePayCount(statisticsBO.getToBePayCount());
                // 代收款
                tallyBO.setToBeReceiptCount(statisticsBO.getToBeReceiptCount());
                // 完成数
                tallyBO.setCompleteCount(statisticsBO.getCompleteCount());
            }

            p.getTallyReport().setSettleAccountPlatform(tallyBO);
        });

        return finalReportList;
    }

    /**
     * 补全数据
     * @param reportList
     * @param statisticsList
     * @param name
     * @return
     */
    private List<MemberTallyReportDO> replenishData(List<MemberTallyReportDO> reportList, List<StatisticsSettlementBO> statisticsList, String name) {
        if (statisticsList == null) {
            logger.info("没有符合条件的{}计数统计数据", name);
            statisticsList = new ArrayList<>();
        }

        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = reportList;
        statisticsList.stream().distinct().forEach(p -> {
            MemberTallyReportDO reportDO = finalReportList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (reportDO == null) {
                reportDO = new MemberTallyReportDO();
                reportDO.setMemberId(p.getMemberId());
                reportDO.setRoleId(p.getRoleId());
                finalReportList.add(reportDO);
            }
        });

        return finalReportList;
    }
}
